#include<QDebug>
#include "mypushbutton.h"
#include<QEasingCurve>
myPushButton::myPushButton(QWidget *parent)
    : QPushButton{parent}
{

}

myPushButton::myPushButton(QString normalImg, QString pressImg)
{
    normalImgPath=normalImg;
    pressImgPath=pressImg;
    QPixmap pixmap;
    bool ret=pixmap.load(normalImgPath);
    if(!ret){
        qDebug()<<normalImg<<"加载图片失败";
        return;
    }
    this->setFixedSize(pixmap.size());
    this->setStyleSheet("QPushButton{border:0px;}");
    this->setIcon(pixmap);
    this->setIconSize(pixmap.size());
}

void myPushButton::zoom1()
{
    QPropertyAnimation* animation1=new QPropertyAnimation(this,"geometry",this);
    animation1->setDuration(200);
    animation1->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
    animation1->setEndValue(QRect(this->x(),this->y()+this->height()*0.1,this->width(),this->height()));
    animation1->setEasingCurve(QEasingCurve::OutBounce);
    animation1->start();
}

void myPushButton::zoom2()
{
    QPropertyAnimation* animation1=new QPropertyAnimation(this,"geometry",this);
    animation1->setDuration(200);
    animation1->setStartValue(QRect(this->x(),this->y()+this->height()*0.1,this->width(),this->height()));
    animation1->setEndValue(QRect(this->x(),this->y(),this->width(),this->height()));
    animation1->setEasingCurve(QEasingCurve::OutBounce);
    animation1->start();
}

void myPushButton::mousePressEvent(QMouseEvent *e)
{
    if(pressImgPath!=""){
        QPixmap pixmap;
        bool ret=pixmap.load(pressImgPath);
        if(!ret){
            qDebug()<<pressImgPath<<"加载图片失败";
            return;
        }
        this->setFixedSize(pixmap.size());
        this->setStyleSheet("QPushButton{border:0px;}");
        this->setIcon(pixmap);
        this->setIconSize(pixmap.size());
    }
    return QPushButton::mousePressEvent(e);

}

void myPushButton::mouseReleaseEvent(QMouseEvent *e)
{
    if(pressImgPath!=""){
        QPixmap pixmap;
        bool ret=pixmap.load(normalImgPath);
        if(!ret){
            qDebug()<<normalImgPath<<"加载图片失败";
            return;
        }
        this->setFixedSize(pixmap.size());
        this->setStyleSheet("QPushButton{border:0px;}");
        this->setIcon(pixmap);
        this->setIconSize(pixmap.size());
    }
    return QPushButton::mouseReleaseEvent (e);
}

